import {observable, extendObservable, action, toJS} from 'mobx';
import {addUserInfo, updateUserInfo} from 'util/api';
import _ from 'lodash';
import {validateFields, getParams} from '../Common.js';
import {message} from 'antd';
import store from 'store';

//导出
export default class AddUnit {
  constructor() {
    this.reset(true);
  }

  @action
  reset = init => {
    const state = {
      fields: {
        cname: {
          value: '',
          require: false,
        },
        recordLocation: {value: '', require: false},
        userType: {value: 0, require: false},
        recordTime: {value: '', require: false},
      },

      imageUrl: require('image/user-photo.png'),
    };

    if (init) {
      extendObservable(this, state);
    } else {
      Object.keys(state).forEach(key => (this[key] = state[key]));
    }
  };

  //取消新增单元
  @action
  resetFields = async () => {
    this.fields = {
      cname: {
        value: '',
        require: false,
      },
      recordLocation: {value: '', require: false},
      userType: {value: 0, require: false},
      recordTime: {value: '', require: false},
    };
  };

  //显示新增单元删除
  @action
  detail = async record => {
    //清空fields
    this.resetFields();
    this.imageUrl = record.photoName
      ? record.photoName
      : require('image/user-photo.png');
    this.currentData = record;
    let obj = toJS(this.fields);
    obj['cname'].value = record.name || '';
    obj['recordLocation'].value = record.recordLocation || '';
    obj['userType'].value = record.userType || 0;
    obj['recordTime'].value = record.recordTime || '';
    this.fields = obj;
    store.CommonModalStore.confim('detail');
  };
}
